/**
 * author      : dormi330
 * date        : 2018/6/24
 * project     : mybatis
 * description : 订单 data access service, 如果有缓存, 则在这一层实现
 */

package com.deep.trade.infrastructure.mybatis.order;

import com.deep.trade.domain.order.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class MybatisOrderRepository implements OrderRepository {

    @Autowired
    private TradeOrderMapper tradeOrderMapper;

    @Autowired
    private OrderSummaryMapper orderSummaryMapper;

    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    @Autowired
    private TradeOrderSkuMapper tradeOrderSkuMapper;

    @Autowired
    private OrderPaymentMapper orderPaymentMapper;

    @Autowired
    private OrderParcelMapper orderParcelMapper;

    public void addOrderSummary(OrderSummary orderSummary) {
        orderSummaryMapper.insert(orderSummary);
    }

    public void addTradeOrder(TradeOrder tradeOrder) {
        tradeOrderMapper.insert(tradeOrder);
    }

    public void addOrderSku(TradeOrderSku tradeOrderSku) {
        tradeOrderSkuMapper.insert(tradeOrderSku);
    }

    public void addOrderDelivery(DeliveryAddress deliveryAddress) {
        deliveryAddressMapper.insert(deliveryAddress);
    }

    public int updateTradeOrderStatus(TradeOrder tradeOrder) {
        return tradeOrderMapper.updateStatus(
                tradeOrder.getTenantCode(), tradeOrder.getId(), tradeOrder.getVersion(),
                // field
                tradeOrder.getStatus(),
                tradeOrder.getAuditRejectReason(),
                tradeOrder.getAuditedAt(),
                tradeOrder.getUpdatedBy()
        );
    }

    public int updateOrderSummaryToPaid(OrderSummary orderSummary) {
        return orderSummaryMapper.updateStatus(
                // condition
                orderSummary.getTenantCode(),
                orderSummary.getId(),
                orderSummary.getVersion(),
                // change field
                orderSummary.getPaidAt(),
                orderSummary.getPayChannel(),
                orderSummary.getUpdatedBy());
    }

    public void addOrderPayInfo(OrderPayment orderPayment) {
        orderPaymentMapper.insert(orderPayment);
    }

    public void addParcel(OrderParcel orderParcel) {
        orderParcelMapper.insert(orderParcel);
    }


}
